package sys.module.lottery.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import sys.module.lottery.dao.IPomeDAO;
import sys.module.lottery.model.PomeModel;
import sys.module.lottery.service.IPomeService;

/**
 * <p>文件名称：PomeServiceImpl.java</p>
 * <p>文件描述：</p>
 * <p>版权所有： 版权所有(C)2000-2010</p>
 * <p>公    司： CWCA</p>
 * <p>内容摘要：TODO </p>
 * <p>其他说明： </p>
 * <p>完成日期：2014年2月15日</p>
 *
 * @author gao x 
 * @version 1.0 $Id: ican_code_comment.xml,v 1.3 2013/07/03 01:47:35 gaoxiang Exp $
 */
@Service("PomeService")
public class PomeServiceImpl implements IPomeService {

	@Autowired
	@Qualifier("PomeDao")
	private IPomeDAO dao;

	@Override
	public List<PomeModel> queryALL() {
		// TODO Auto-generated method stub
		try {
			return dao.queryAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean updateModel(PomeModel model) {
		// TODO Auto-generated method stub
		String text = "";
		if (model != null) {
			if (model.getText() == null) {
				text += model.getPome_1() + "#" + model.getPome_2() + "#"
						+ model.getPome_3() + "#" + model.getPome_4();
				model.setText(text);
			}
			try {
				dao.updateModel(model);
				if (!isUpdata(model)) {
					this.updateModel(model);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * <p>功能描述：判断数据是否插入 TODO</p>
	 * <p>创建人：liao x</p>
	 * <p>创建日期：2014年2月16日</p>
	 *
	 * @param model
	 * @return
	 * @throws InterruptedException 
	 */
	private boolean isUpdata(PomeModel model) throws InterruptedException {
		if (model == null)
			return false;
		PomeModel info = this.queryMax(model.getId());
		Thread.sleep(10);
		if (info != null) {
			if (model.getText().equals(info.getText()))
				return true;
		}
		return false;
	}

	@Override
	public PomeModel queryMax(int id) {
		// TODO Auto-generated method stub
		try {
			List<PomeModel> list = dao.queryAll();
			if (list != null && list.size() > 0)
				for (PomeModel pomeModel : list) {
					if (pomeModel.getId() == id)
						return pomeModel;
				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
